package java学习.第11章_常用类和基础API;

public class H_Runtime类 {
    public static void main(String[] args) {
        Runtime r = Runtime.getRuntime();//单例模式

        long tm = r.totalMemory();//获取虚拟机初始化时堆内存总容量
        long mm = r.maxMemory();//获取虚拟机最大时堆内存总容量
        long fm = r.freeMemory();//堆 内空闲内存

        RuntimeTest(tm, "堆内存总容量");
        RuntimeTest(mm, "堆内存最大容量");
        RuntimeTest(fm, "空闲容量");
        RuntimeTest(tm - fm, "已用容量");

    }

    static String[] dw = {"B\t", "KB\t", "MB\t", "GB\t", "TB\t"};

    private static void RuntimeTest(long m, String s) {
        System.out.print(s + ":\t");
        for (int i = 0; m > 0; i++) {
            long ii = (m & 1023);
            if (ii != 0) {
                System.out.print(ii + dw[i]);
            }
            m >>= 10;
        }
        System.out.println();
    }

}
